package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.AgencyLogic;
import com.hotel.BL.CreditCardLogic;
import com.hotel.BL.POSLogic;
import com.hotel.BL.ReservationLogic;
import com.hotel.BL.SalutationLogic;

import data.Agency;
import data.CreditCard;
import data.POS;
import data.PosCommissionConsole;
import data.Salutation;
import data.ValidityConsole;
import data.passingData.ResultData;
@SuppressWarnings("serial")
public class AgencyService extends HttpServlet {
		//http://localhost:8080/agencyService?func=addagency&hotelId=1&agency=aa&organization=tt
		//http://localhost:8080/agencyService?func=editagency&modearrivalId=874&title=12345&description=aabbb
		//http://localhost:8080/agencyService?func=delagency&modearrivalId=874
		//http://localhost:8080/agencyService?func=loadagency&modearrivalId=874
		public void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws IOException {
			try{
				String doClass = req.getParameter("func");
				if(doClass.equals("addagency")){
					AddAgency(req,resp);
				}else if(doClass.equals("addagencyadmin")){
					AddAgencyAdmin(req,resp);
				}else if(doClass.equals("addcorporate")){
					AddCorporate(req,resp);
				}else if(doClass.equals("editagency")){
					EditAgency(req,resp);
				}else if(doClass.equals("editcorporate")){
					EditCorporate(req,resp);
				}else if(doClass.equals("editagentadmin")){
					EditAgentAdmin(req,resp);
				}else if(doClass.equals("delagency")){
					DeleteAgency(req,resp);
				}else if(doClass.equals("editStatusCorporateById")){
					EditstatusCorporate(req,resp);
				}else if(doClass.equals("delcorporate")){
					DeleteCorporate(req,resp);
				}else if(doClass.equals("loadagency")){
					LoadAgency(req,resp);
				}else if(doClass.equals("getCorporateById")){
					GetCorporateById(req,resp);
				}else if(doClass.equals("getCorporate")){
					GetCorporate(req,resp,"corporate");
				}else if(doClass.equals("getAgency")){
					GetCorporate(req,resp,"agent");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		private void LoadAgency(HttpServletRequest req, HttpServletResponse resp) throws IOException {
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}
			long agencyId = Long.parseLong(req.getParameter("agencyId"));
			Agency obj = null;
			if(agencyId != 0){
				 obj = new AgencyLogic(req,resp).GetAgency(agencyId);
			}else{
				obj = null;
			}
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(obj));
		}
		
		private void GetCorporateById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}
			long agencyId = Long.parseLong(req.getParameter("agencyId"));
			Agency obj = null;
			HashMap<String,Object> agency = new HashMap<String,Object>();
			if(agencyId != 0){
				obj = new AgencyLogic(req, resp).GetAgency(agencyId);
				agency.put("avaliable",obj.isAvaliable());
				agency.put("agent",obj.getAgency());
				agency.put("billaddress",obj.getBilladdress());
				agency.put("billcity",obj.getBillcity());
				agency.put("billcountry",obj.getBillcountry());
				agency.put("billstate",obj.getBillstate());
				agency.put("billtitle",obj.getBilltitle());
				agency.put("billzipcode",obj.getBillzipcode());
				agency.put("contactdesignation",obj.getContactdesignation());
				agency.put("contactdob",obj.getContactdob());
				agency.put("contactemail",obj.getContactemail());
				agency.put("contactextend",obj.getContactextend());
				agency.put("contactfirstname",obj.getContactfirstname());
				agency.put("contactlastname",obj.getContactlastname());
				agency.put("contactmobile",obj.getContactmobile());
				agency.put("contactofficefax",obj.getContactofficefax());
				agency.put("contactofficephone",obj.getContactofficephone());
				if(obj.getContactsalutaion()!=null){
					Salutation contact = new SalutationLogic().GetSalutationById(Long.parseLong(obj.getContactsalutaion()));
					agency.put("contactsalutationStr",contact.getTitle());
				}
				agency.put("contactsalutaion",obj.getContactsalutaion());
				agency.put("contactwebsite",obj.getContactwebsite());
				agency.put("contactgender",obj.getContactgender());
				agency.put("corporate",obj.isCorporate());
				agency.put("designationperson",obj.getDesignationperson());
				agency.put("displayId",obj.getDisplayId());
				agency.put("dobperson",obj.getDobperson());
				agency.put("emailperson",obj.getEmailperson());
				agency.put("extendperson",obj.getExtendperson());
				agency.put("firstnameperson",obj.getFirstnameperson());
				agency.put("genderperson",obj.getGenderperson());
				agency.put("hotelId",obj.getHotelId());
				agency.put("id",obj.getId());
				if(obj.getSalutationperson()!=null){
					Salutation note = new SalutationLogic().GetSalutationById(Long.parseLong(obj.getSalutationperson()));
					agency.put("salutationpersonStr",note.getTitle());
				}
				agency.put("lastnameperson",obj.getLastnameperson());
				agency.put("mainaddress",obj.getMainaddress());
				agency.put("maincity",obj.getMaincity());
				agency.put("maincountry",obj.getMaincountry());
				agency.put("mainstate",obj.getMainstate());
				agency.put("mainzipcode",obj.getMainzipcode());
				agency.put("mobileperson",obj.getMobileperson());
				agency.put("officefaxperson",obj.getOfficefaxperson());
				agency.put("organization",obj.getOrganization());
				agency.put("phoneofficeperson",obj.getPhoneofficeperson());
				agency.put("salutationperson",obj.getSalutationperson());
				agency.put("status",obj.isStatus());
				agency.put("websiteperson",obj.getWebsiteperson());
				agency.put("listcreditString",obj.getListcreditString());
				if(obj.getListcreditString()!=null){
					String[] splitListCredit = obj.getListcreditString().split(",");
					List<Object> credetlist = new ArrayList<Object>();
					for(int i =0;i<splitListCredit.length;i++){
						CreditCard cc = new CreditCardLogic(req, resp).GetCreditCard(Long.parseLong(splitListCredit[i]));
						credetlist.add(cc);
					}
					agency.put("listcredit",credetlist);
				}
				if(obj.getListValidity()!=null&&!obj.getListValidity().equals("")){
					String[] splitListValidity = obj.getListValidity().split(",");
					List<Object> validitylist = new ArrayList<Object>();
					for(int i =0;i<splitListValidity.length;i++){
						ValidityConsole vc = new AgencyLogic(req, resp).GetValidityConsole(Long.parseLong(splitListValidity[i]));
						if(vc.isAvaliable())
							validitylist.add(vc);
					}
					agency.put("listValidity",validitylist);
				}
				if(obj.getPoscommission()!=null&&!obj.getPoscommission().equals("")){
					String[] splitListposcommis = obj.getPoscommission().split(",");
					List<HashMap<String,Object>> poscommislist = new ArrayList<HashMap<String,Object>>();
					for(int i =0;i<splitListposcommis.length;i++){
						HashMap<String,Object> poscommisset = new HashMap<String,Object>();
						PosCommissionConsole pm = new AgencyLogic(req, resp).GetPOSCommissionConsole(Long.parseLong(splitListposcommis[i]));
						poscommisset.put("id", pm.getId());
						poscommisset.put("pospointId", pm.getPospointId());
						POS pos = new POSLogic(req,resp).GetPOS(pm.getPospointId());
						poscommisset.put("titlepospoint", pos.getTitle());
						poscommisset.put("consoleId", pm.getConsoleId());
						poscommisset.put("discount", pm.getDiscount());
						if(pm.isAvaliable()){
							poscommislist.add(poscommisset);
						}
					}
					agency.put("listposcommis",poscommislist);
				}
				agency.put("discount",obj.getDiscount());
				agency.put("creditlimit",obj.getCreditlimit());
				agency.put("paymentterms",obj.getPaymentterms());
			}else{
				obj = null;
			}
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(agency));
		}
		
		private void GetCorporate(HttpServletRequest req, HttpServletResponse resp,String type) throws IOException, JSONException, ServletException, EntityNotFoundException
		{	
			HttpSession session=req.getSession(true);
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}
			long hotelId = (Long) session.getAttribute("hotelId");
			long offset = Long.parseLong(req.getParameter("offset"));
			long line = Long.parseLong(req.getParameter("line"));
			long pointer = Long.parseLong(req.getParameter("pagePointer"));
			List<Agency> obj = new AgencyLogic(req,resp).GetAllCorporate4AdminLimit(hotelId,offset,line,type);
			double dataSize = new AgencyLogic(req,resp).GetAllCorporate4Admin(hotelId,type).size();
			HashMap<String,Object> data = new HashMap<String,Object>();
			data.put("dataAmount",dataSize);
			data.put("pageNumber",pointer);
			data.put("lineAmount", line);
			data.put("pageAmount", Math.ceil(dataSize/line));
			data.put("data", obj);
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(data));
		}

		private void DeleteAgency(HttpServletRequest req,
				HttpServletResponse resp) throws EntityNotFoundException, IOException {
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}
			long agencyId = Long.parseLong(req.getParameter("agencyId"));
			
			Agency obj = new AgencyLogic(req,resp).DeleteAgency(agencyId);

			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(obj));
			
		}
		
		private void EditstatusCorporate(HttpServletRequest req,
			HttpServletResponse resp) throws EntityNotFoundException, IOException {
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}
			String agencyId = req.getParameter("id");
			Agency obj = null;
			obj = new AgencyLogic(req,resp).EditStatus(Long.parseLong(agencyId));
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(obj));
		}
		
		private void DeleteCorporate(HttpServletRequest req,
				HttpServletResponse resp) throws EntityNotFoundException, IOException {
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}
			String agencyId = req.getParameter("agencyId");
			Agency obj = null;
			for(int i = 0; i < agencyId.split(",").length ; i++){
				obj = new AgencyLogic(req,resp).DeleteAgency(Long.parseLong(agencyId.split(",")[i]));
			}
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(obj));
		}
		
		private void EditAgency(HttpServletRequest req, HttpServletResponse resp) throws EntityNotFoundException, IOException, NumberFormatException, ParseException {
			HttpSession session=req.getSession(true);
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}	
			long hotelId = (Long) session.getAttribute("hotelId");
			long agencyId = Long.parseLong(req.getParameter("agencyId"));
			String agency = req.getParameter("agency");
			String organization = req.getParameter("organization");
			long groupId = Long.parseLong(req.getParameter("groupId"));
			long resId = Long.parseLong(req.getParameter("resId"));
			Agency agc = new Agency();
			if(agencyId == 0){
				long displayID = new AgencyLogic(req,resp).GetMaxAgentDisplayId(hotelId);
				 agc =new AgencyLogic(req,resp).AddAgency(agencyId,agency,organization,(displayID+1));
				 if(groupId != 0){
					 new ReservationLogic(req,resp).EditAgencyGroup(groupId,agc.getId());
				 }else{
					 new ReservationLogic(req,resp).EditAgency(resId,agc.getId());
				 }
			}else{
				 agc =new AgencyLogic(req,resp).EditAgency(agencyId,agency,organization);
			}
			
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(agc));
		}
		
		private void EditAgentAdmin(HttpServletRequest req, HttpServletResponse resp) throws EntityNotFoundException, IOException, NumberFormatException, ParseException {
			/*long id = Long.parseLong(req.getParameter("id"));
			String title = req.getParameter("title");
			String country = req.getParameter("country");
			String state = req.getParameter("state");
			String phone = req.getParameter("phone");
			String email = req.getParameter("email");
			Agency aa = new AgencyLogic(req,resp).EditCorporate(id,title,country,state,phone,email);
			Gson gson = new Gson();
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(aa));*/
		}

		private void AddAgency(HttpServletRequest req, HttpServletResponse resp) throws EntityNotFoundException, IOException {
			HttpSession session=req.getSession(true);
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}	
			long hotelId = (Long) session.getAttribute("hotelId");
			String agency = req.getParameter("agency");
			String organization = req.getParameter("organization");
			long displayID = new AgencyLogic(req,resp).GetMaxAgentDisplayId(hotelId);
			Agency agc =new AgencyLogic(req,resp).AddAgency(hotelId,agency,organization,(displayID+1));
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(agc));
		}
		
		private void AddAgencyAdmin(HttpServletRequest req, HttpServletResponse resp) throws EntityNotFoundException, IOException {
			HttpSession session=req.getSession(true);
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}	
			long hotelId = (Long) session.getAttribute("hotelId");
			String title = req.getParameter("title");
			String country = req.getParameter("country");
			String state = req.getParameter("state");
			String phone = req.getParameter("phone");
			String email = req.getParameter("email");
			long displayID = new AgencyLogic(req,resp).GetMaxAgentDisplayId(hotelId);
			Agency agc = new AgencyLogic(req,resp).AddAgencyAdmin(hotelId,title,country,state,phone,email,(displayID+1));
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(agc));
		}
		
		private void AddCorporate(HttpServletRequest req, HttpServletResponse resp) throws EntityNotFoundException, IOException, ParseException {
			HttpSession session=req.getSession(true);
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
					Locale.ENGLISH);
			long hotelId = (Long) session.getAttribute("hotelId");
			String title = req.getParameter("title");
			//main
			String address_main = req.getParameter("address_main");
			String country_main = req.getParameter("country_main");
			String city_main = req.getParameter("city_main");
			String state_main = req.getParameter("state_main");
			String zipcode_main = req.getParameter("zipcode_main");
			//person
			String salutation_person = req.getParameter("salutation_person");
			String firstname_person = req.getParameter("firstname_person");
			String lastname_person = req.getParameter("lastname_person");
			String designation_person = req.getParameter("designation_person");
			String phone_person = req.getParameter("phone_person");
			String extn_person = req.getParameter("extn_person");
			String officefax_person = req.getParameter("officefax_person");
			String email_person = req.getParameter("email_person");
			String mobile_person = req.getParameter("mobile_person");
			String gender_person = req.getParameter("gender_person");
			Date dateofbirth_person = formatter.parse(req.getParameter("dateofbirth_person"));
			String websiteperson = req.getParameter("websiteperson");
			//billing
			String titlebill = req.getParameter("title_bill");
			String addressbill = req.getParameter("address_bill");
			String country2 = req.getParameter("country2");
			String state2 = req.getParameter("state2");
			String citybill = req.getParameter("city_bill");
			String zipcodebill = req.getParameter("zipcode_bill");
			//billing contact
			String salutationbill_contact = req.getParameter("salutationbill_contact");
			String firstname_contactperson = req.getParameter("firstname_contactperson");
			String lastname_contactperson = req.getParameter("lastname_contactperson");
			String designation_contactperson = req.getParameter("designation_contactperson");
			String officephone_contactperson = req.getParameter("officephone_contactperson");
			String extn_contactperson = req.getParameter("extn_contactperson");
			String officefax_contactperson = req.getParameter("officefax_contactperson");
			String email_contactperson = req.getParameter("email_contactperson");
			String mobile_contactperson = req.getParameter("mobile_contactperson");
			String gender_contactperson = req.getParameter("gender_contactperson");
			Date dateofbirth_contactperson = formatter.parse(req.getParameter("dateofbirth_contactperson"));
			String website_contactperson = req.getParameter("website_contactperson");
			String creditlimit = req.getParameter("creditlimit");
			String paymentterms = req.getParameter("paymentterms");
			String commission = req.getParameter("commission");
			String type = req.getParameter("type");
			long displayID = new AgencyLogic(req, resp).GetMaxCorporateDisplayId(hotelId,type);
			Agency agc =new AgencyLogic(req, resp).AddCorporate(hotelId,title,address_main,country_main,city_main,state_main,zipcode_main
					,salutation_person,firstname_person,lastname_person,designation_person,phone_person,extn_person,officefax_person,email_person,
					mobile_person,gender_person,dateofbirth_person,websiteperson,titlebill,addressbill,country2,state2,citybill,zipcodebill
					,salutationbill_contact,firstname_contactperson,lastname_contactperson,designation_contactperson,officephone_contactperson
					,extn_contactperson,officefax_contactperson,email_contactperson,mobile_contactperson,gender_contactperson,dateofbirth_contactperson
					,website_contactperson,creditlimit,paymentterms,commission,type,(displayID+1));
			//credit card
			String ccFName = req.getParameter("ccFName");
			String ccNo = req.getParameter("ccNo");
			String ccType = req.getParameter("ccType");
			String expiryMonth = req.getParameter("expiryMonth");
			String expiryYear = req.getParameter("expiryYear");
			String ExpiryDate = expiryMonth+"/"+expiryYear;
			String cvc = req.getParameter("cvc");
			String ccAddress1 = req.getParameter("ccAddress1");
			String country3 = req.getParameter("country3");
			String state3 = req.getParameter("state3");
			String ccCity = req.getParameter("ccCity");
			String ccZipCode = req.getParameter("ccZipCode");
			if(!ccFName.equals("")){
				new CreditCardLogic(req,resp).AddConsoleCreditCard( hotelId,  cvc,  ccType,ccFName,  ccNo,  ExpiryDate,
						ccAddress1, state3, country3, ccCity, ccZipCode,agc.getId());
			}
			//Validity
			String objvalidityDate = req.getParameter("objvalidityDate");
			if(objvalidityDate!=null&&!objvalidityDate.equals("")){
				String[] sizeObjValidity = objvalidityDate.split(",");
				String[] splitDate = {};
				for(int i =0;i<sizeObjValidity.length;i++){
					splitDate = sizeObjValidity[i].split("'");
					new AgencyLogic(req, resp).AddValidityConsole(hotelId, formatter.parse(splitDate[0]), formatter.parse(splitDate[1]), agc.getId());
				}
			}
			//poscommission
			String objaddposcommis = req.getParameter("objaddposcommis");
			if(objaddposcommis!=null&&!objaddposcommis.equals("")){
				String[] sizeObjposcommis = objaddposcommis.split(",");
				String[] splitposcommis = {};
				for(int i=0;i<sizeObjposcommis.length;i++){
					splitposcommis = sizeObjposcommis[i].split("'");
					new AgencyLogic(req, resp).AddPOSCommissionConsole(hotelId, Long.parseLong(splitposcommis[0]),Double.valueOf(splitposcommis[1]), agc.getId());
				}
			}
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(agc));
		}
		
		private void EditCorporate(HttpServletRequest req, HttpServletResponse resp) throws EntityNotFoundException, IOException, NumberFormatException, ParseException {
			HttpSession session=req.getSession(true);
			Gson gson = new Gson();
			String sessionCheck = Util.checkSessionEntity(req,resp);
			if(sessionCheck==null||sessionCheck.equals("")){
				resp.setContentType("text/plain; charset=UTF-8");
				resp.getWriter().println(gson.toJson(new ResultData(false)));
				return;
			}
			long id = Long.parseLong(req.getParameter("id"));
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
					Locale.ENGLISH);
			long hotelId = (Long) session.getAttribute("hotelId");
			String type = req.getParameter("type");
			String title = req.getParameter("title");
			//main
			String address_main = req.getParameter("address_main");
			String country_main = req.getParameter("country_main");
			String city_main = req.getParameter("city_main");
			String state_main = req.getParameter("state_main");
			String zipcode_main = req.getParameter("zipcode_main");
			//person
			String salutation_person = req.getParameter("salutation_person");
			String firstname_person = req.getParameter("firstname_person");
			String lastname_person = req.getParameter("lastname_person");
			String designation_person = req.getParameter("designation_person");
			String phone_person = req.getParameter("phone_person");
			String extn_person = req.getParameter("extn_person");
			String officefax_person = req.getParameter("officefax_person");
			String email_person = req.getParameter("email_person");
			String mobile_person = req.getParameter("mobile_person");
			String gender_person = req.getParameter("gender_person");
			Date dateofbirth_person = formatter.parse(req.getParameter("dateofbirth_person"));
			String websiteperson = req.getParameter("websiteperson");
			//billing
			String titlebill = req.getParameter("title_bill");
			String addressbill = req.getParameter("address_bill");
			String country2 = req.getParameter("country2");
			String state2 = req.getParameter("state2");
			String citybill = req.getParameter("city_bill");
			String zipcodebill = req.getParameter("zipcode_bill");
			//billing contact
			String salutationbill_contact = req.getParameter("salutationbill_contact");
			String firstname_contactperson = req.getParameter("firstname_contactperson");
			String lastname_contactperson = req.getParameter("lastname_contactperson");
			String designation_contactperson = req.getParameter("designation_contactperson");
			String officephone_contactperson = req.getParameter("officephone_contactperson");
			String extn_contactperson = req.getParameter("extn_contactperson");
			String officefax_contactperson = req.getParameter("officefax_contactperson");
			String email_contactperson = req.getParameter("email_contactperson");
			String mobile_contactperson = req.getParameter("mobile_contactperson");
			String gender_contactperson = req.getParameter("gender_contactperson");
			Date dateofbirth_contactperson = formatter.parse(req.getParameter("dateofbirth_contactperson"));
			String website_contactperson = req.getParameter("website_contactperson");
			String creditlimit = req.getParameter("creditlimit");
			String paymentterms = req.getParameter("paymentterms");
			String commission = req.getParameter("commission");
			Agency aa = new AgencyLogic(req, resp).EditCorporate(id,title,address_main,country_main,city_main,state_main,zipcode_main
					,salutation_person,firstname_person,lastname_person,designation_person,phone_person,extn_person,officefax_person,email_person,
					mobile_person,gender_person,dateofbirth_person,websiteperson,titlebill,addressbill,country2,state2,citybill,zipcodebill
					,salutationbill_contact,firstname_contactperson,lastname_contactperson,designation_contactperson,officephone_contactperson
					,extn_contactperson,officefax_contactperson,email_contactperson,mobile_contactperson,gender_contactperson,dateofbirth_contactperson
					,website_contactperson,creditlimit,paymentterms,commission,type);
			//credit card
			String ccFName = req.getParameter("ccFName");
			String ccNo = req.getParameter("ccNo");
			String ccType = req.getParameter("ccType");
			String expiryMonth = req.getParameter("expiryMonth");
			String expiryYear = req.getParameter("expiryYear");
			String ExpiryDate = expiryMonth+"/"+expiryYear;
			String cvc = req.getParameter("cvc");
			String ccAddress1 = req.getParameter("ccAddress1");
			String country3 = req.getParameter("country3");
			String state3 = req.getParameter("state3");
			String ccCity = req.getParameter("ccCity");
			String ccZipCode = req.getParameter("ccZipCode");
			
			if(!ccFName.equals("")){
				new CreditCardLogic(req,resp).AddConsoleCreditCard(hotelId,  cvc,  ccType,ccFName,  ccNo,  ExpiryDate,
						ccAddress1, state3, country3, ccCity, ccZipCode,id);
			}
			//Validity
			String objvalidityDate = req.getParameter("objvalidityDate");
			if(objvalidityDate!=null&&!objvalidityDate.equals("")){
				String[] sizeObjValidity = objvalidityDate.split(",");
				String[] splitDate = {};
				for(int i =0;i<sizeObjValidity.length;i++){
					splitDate = sizeObjValidity[i].split("'");
					if(splitDate[2]!=null&&!splitDate[2].equals("undefined")&&!splitDate[3].equals("DELETE")&&splitDate[3].equals("ADD")){
						new AgencyLogic(req, resp).EditValidityConsole(formatter.parse(splitDate[0]), formatter.parse(splitDate[1]), Long.parseLong(splitDate[2]));
					}else if(splitDate[3].equals("DELETE")){
						new AgencyLogic(req,resp).DeleteValidityConsole(Long.parseLong(splitDate[2]),id);
					}else{
						new AgencyLogic(req, resp).AddValidityConsole(hotelId, formatter.parse(splitDate[0]), formatter.parse(splitDate[1]), id);
					}
				}
			}
			//poscommission
			String objaddposcommis = req.getParameter("objaddposcommis");
			if(objaddposcommis!=null&&!objaddposcommis.equals("")){
				String[] sizeObjposcommis = objaddposcommis.split(",");
				String[] splitposcommis = {};
				for(int i=0;i<sizeObjposcommis.length;i++){
					splitposcommis = sizeObjposcommis[i].split("'");
					if(splitposcommis[2]!=null&&!splitposcommis[2].equals("undefined")&&!splitposcommis[3].equals("DELETE")&&splitposcommis[3].equals("ADD")){
						new AgencyLogic(req, resp).EditPosCommissionConsole(Double.valueOf(splitposcommis[1]),Long.parseLong(splitposcommis[2]));
					}else if(splitposcommis[3].equals("DELETE")){
						new AgencyLogic(req, resp).DeletePosCommissionConsole(Long.parseLong(splitposcommis[2]),id);
					}else{
						new AgencyLogic(req, resp).AddPOSCommissionConsole(hotelId, Long.parseLong(splitposcommis[0]),Double.valueOf(splitposcommis[1]), id);
					}
				}
			}
			//delCC
			String delCC = req.getParameter("delCC");
			if(delCC!=null&&!delCC.equals("")){
				String[] delCCList = delCC.split(",");
				for(int i =0;i<delCCList.length;i++){
					new CreditCardLogic(req, resp).DeleteCreditCardConsole(Long.parseLong(delCCList[i]),id);
				}
			}
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(aa));
		}
}

